#include "dictationfile.h"

#include <QFile>
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
#include <QMessageBox>

DictationFile::DictationFile() :
        m_unsaved(true),
        m_filepath("")
{
}

DictationFile::DictationFile(const QString &filepath) :
        m_unsaved(false),
        m_filepath(filepath)
{
    this->loadXmlFilePath(filepath);
}

void DictationFile::loadXmlFilePath(const QString &filepath)
{
    m_filepath = filepath;
    QFile *xmlFile = new QFile(filepath);

    if (!xmlFile->open(QIODevice::ReadOnly | QIODevice::Text)) {
        QMessageBox::critical(NULL, "QXSRExample::parseXML", "Couldn't open example.xml", QMessageBox::Ok);
        return;
    }

    QXmlStreamReader reader(xmlFile);

    QMap<QString, QString> map;

    while (!reader.atEnd() && !reader.hasError()) {
        QXmlStreamReader::TokenType token = reader.readNext();

        if(token == QXmlStreamReader::StartDocument)
            continue;

        if(token == QXmlStreamReader::StartElement) {
            if (reader.name() == "Dictation")
                continue;

            if (reader.name() == "Title"
                || reader.name() == "MmsUrl"
                || reader.name() == "LocalAsfFile"
                || reader.name() == "Transcript"
                || reader.name() == "Answer") {
                addElementDataToMap(reader, map);
            }
        }
    }

    QMap<QString, QString>::const_iterator iter = map.constBegin();
    while (iter != map.constEnd()) {
        if (iter.key() == "Title") {
            m_title = iter.value();
        }
        else if (iter.key() == "MmsUrl") {
            m_mmsUrl = iter.value();
        }
        else if (iter.key() == "LocalAsfFile") {
            m_localAsfPath = iter.value();
        }
        else if (iter.key() == "Transcript") {
            m_transcript = iter.value();
        }
        else if (iter.key() == "Answer") {
            m_answer = iter.value();
        }
        iter++;
    }

    xmlFile->close();
    delete xmlFile;

    m_unsaved = false;
}

void DictationFile::addElementDataToMap(QXmlStreamReader &reader, QMap<QString, QString>& map)
{
    if(reader.tokenType() != QXmlStreamReader::StartElement)
        return;

    QString elementName = reader.name().toString();
    reader.readNext();
    if(reader.tokenType() != QXmlStreamReader::Characters)
        return;

    map.insert(elementName, reader.text().toString());
}

void DictationFile::saveXmlFilePath(const QString &filepath)
{
    m_filepath = filepath;
    QFile *xmlFile = new QFile(filepath);
    if (!xmlFile->open(QIODevice::WriteOnly | QIODevice::Text)) {
        QMessageBox::critical(NULL, "QXSRExample::parseXML", "Couldn't open example.xml", QMessageBox::Ok);
        return;
    }

    QXmlStreamWriter writer(xmlFile);
    writer.setAutoFormatting(true);
    writer.writeStartDocument();

    writer.writeStartElement("Dictation");

    writer.writeTextElement("Title", m_title);
    writer.writeTextElement("MmsUrl", m_mmsUrl);
    writer.writeTextElement("LocalAsfFile", m_localAsfPath);
    writer.writeTextElement("Transcript", m_transcript);
    writer.writeTextElement("Answer", m_answer);

    writer.writeEndElement();

    writer.writeEndDocument();

    xmlFile->close();

    m_unsaved = false;
}

void DictationFile::reset()
{
    m_unsaved = true;
    m_title = "";
    m_transcript = "";
    m_answer = "";
    m_mmsUrl = "";
    m_localAsfPath = "";
    m_filepath = "";
}

QString DictationFile::title()
{
    return m_title;
}

QString DictationFile::transcript()
{
    return m_transcript;
}

QString DictationFile::answer()
{
    return m_answer;
}

QString DictationFile::mmsUrl()
{
    return m_mmsUrl;
}

QString DictationFile::localAsfPath()
{
    return m_localAsfPath;
}

QString DictationFile::filePath()
{
    return m_filepath;
}

bool DictationFile::isUnSaved()
{
    return m_unsaved;
}

void DictationFile::setTitle(const QString &title)
{
    m_title = title;
}

void DictationFile::setTranscript(const QString &transcript)
{
    m_transcript = transcript;
}

void DictationFile::setAnswer(const QString &answer)
{
    m_answer = answer;
}

void DictationFile::setMmsURL(const QString &url)
{
    m_mmsUrl = url;
}

void DictationFile::setLocalAsfPath(const QString &path)
{
    m_localAsfPath = path;
}
